﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Linq;

namespace RDIFramework.WebApp
{
    using RDIFramework.WebApp.CommonUtils;
    using RDIFramework.BusinessEntity;
    using RDIFramework.ServiceAdapter;

    public partial class _Default : BasePage
    {
        protected string menuJSON = "", navContent="";
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (DateTime.Now.Month == 8 && DateTime.Now.Day > 30)
            //{
            //    Response.Write("<h1>试用期到啦</h2>");
            //    Response.End();
            //}

            base.GetModuleDT();
            if (base.DTModule != null && base.DTModule.Rows.Count > 0)
            {
                menuJSON = GetTreeJsonByTable(base.DTModule, "Id", "FullName", "ParentId", "1");
            }           

            if (menuJSON == "")
                menuJSON = "null";

            string themePath = Server.MapPath("~/theme/navtype/");
            NVelocityUtils vel = new NVelocityUtils(themePath);
            vel.Put("username", base.UserInfo.UserName);
            string navHTML = "Accordion.HTML";
            navContent = vel.FileToString(navHTML);
        }

        /// <summary>
        /// 根据DataTable生成Json树结构
        /// </summary>
        /// <param name="tabel">数据源</param>
        /// <param name="idCol">ID列</param>
        /// <param name="txtCol">Text列</param>
        /// <param name="rela">关系字段</param>
        /// <param name="pId">父ID</param>
        /// <returns>返回json数据</returns>
        string result = string.Empty;
        string sb = string.Empty;
        private string GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string rela, object pId)
        {
            result += sb;
            sb = string.Empty;

            if (tabel.Rows.Count > 0)
            {
                sb += "[";
                string filer = string.Format("{0}='{1}'", rela, pId);
                DataRow[] rows = tabel.Select(filer);
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        sb += "{\"Id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol]                            
                           + "\",\"NavigateUrl\":\"" + row[PiModuleTable.FieldNavigateUrl]
                           + "\",\"IconCss\":\"" + row[PiModuleTable.FieldIconCss]
                           + "\",\"FullName\":\"" + row[PiModuleTable.FieldFullName]
                           + "\",\"state\":\"open\"";
                        if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
                        {
                            sb += ",\"children\":";
                            GetTreeJsonByTable(tabel, idCol, txtCol, rela, row[idCol]);
                            result += sb;
                            sb = string.Empty;
                        }
                        result += sb;
                        sb = string.Empty;
                        sb += "},";
                    }
                    sb = sb.Remove(sb.Length - 1, 1);
                }
                sb += "]";
                result += sb;
                sb = string.Empty;
            }
            return result;
        }
    }
}